package com.dswz.controller.pc;

import com.dswz.po.User;
import com.dswz.service.SessionService;
import com.dswz.service.UserService;
import com.dswz.utils.AdminResult;
import com.dswz.utils.EncryptUtil;
import com.dswz.utils.RequestUtils;
import com.dswz.utils.Utils;
import com.dswz.vo.VoUser;
import com.google.gson.Gson;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping({ "/pcs" })
public class UserLoginPcController {
	private Logger logger = LogManager.getLogger(UserLoginPcController.class);

	@Autowired
	private UserService userService;

	@Autowired
	private SessionService sessionService;

	@RequestMapping(value = { "/editUser" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET })
	@ResponseBody
	public Object editUser(String userId, Model model, HttpServletRequest request, HttpServletResponse response) {
		long startTime = System.currentTimeMillis();
		AdminResult result = new AdminResult();
		try {
			String username = this.sessionService.getAttibute(RequestUtils.getCSESSIONID(request, response),
					"USER_SESSION");
			this.logger.info("userId:" + userId + "页面来源:" + request.getHeader("referer") + "客户端:"
					+ request.getHeader("user-agent") + " 客户端ip: " + Utils.getUserRealAddress(request) + " 操作人: "
					+ username);
			VoUser voUser = this.userService.editUser(userId);
			result.setData(voUser);
			result.setResult(0);
			result.setMessage("OK");
			long endTime = System.currentTimeMillis();
			this.logger.info("result:" + new Gson().toJson(result) + "程序运行时间" + (endTime - startTime) + "ms");
		} catch (Exception e) {
			this.logger.error(e.getMessage(), e);
			result.setMessage(e.getMessage());
			result.setResult(2);
		}
		return result;
	}

	@RequestMapping(value = { "/applyAuth" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public Object applyAuth(String userId, String userName, Byte userCredentialsType, String userCredentialsNum,
			Model model, HttpServletRequest request, HttpServletResponse response) {
		long startTime = System.currentTimeMillis();
		AdminResult result = new AdminResult();
		try {
			String username = this.sessionService.getAttibute(RequestUtils.getCSESSIONID(request, response),
					"USER_SESSION");
			this.logger.info("userId:" + userId + "userName:" + userName + "userCredentialsType:" + userCredentialsType
					+ "userCredentialsNum:" + userCredentialsNum + "页面来源:" + request.getHeader("referer") + "客户端:"
					+ request.getHeader("user-agent") + " 客户端ip: " + Utils.getUserRealAddress(request) + " 操作人: "
					+ username);
			User user = new User();
			user.setUserId(userId);
			user.setUserName(username);
			user.setUserCredentialsType(userCredentialsType);
			user.setUserCredentialsNum(userCredentialsNum);
			user.setUserAuthStatus(Byte.valueOf((byte) 1));
			user.setUserAuthTime(new Date());
			this.userService.updateUser(user);
			result.setResult(0);
			result.setMessage("OK");
			long endTime = System.currentTimeMillis();
			this.logger.info("result:" + new Gson().toJson(result) + "程序运行时间" + (endTime - startTime) + "ms");
		} catch (Exception e) {
			this.logger.error(e.getMessage(), e);
			result.setMessage(e.getMessage());
			result.setResult(2);
		}
		return result;
	}

	@RequestMapping(value = { "/changePassword" }, method = {
			org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public Object changePassword(String userId, String oldPassword, String newPassword, String confirmPassword,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		long startTime = System.currentTimeMillis();
		AdminResult result = new AdminResult();
		try {
			String username = this.sessionService.getAttibute(RequestUtils.getCSESSIONID(request, response),
					"USER_SESSION");
			this.logger.info(/* 127 */ "userId:" + userId + "oldPassword:" + oldPassword + "newPassword:" + newPassword
					+ "confirmPassword:" + confirmPassword + "页面来源:" + request.getHeader("referer") + "客户端:"
					+ request.getHeader("user-agent") + " 客户端ip: " + Utils.getUserRealAddress(request) + " 操作人: "
					+ username);
			if (StringUtils.isEmpty(oldPassword)) {
				this.logger.error("请输入旧密码!");
				result.setMessage("请输入旧密码!");
				result.setResult(3);
				return result;
			}
			User user = this.userService.getUserById(userId);
			if (!EncryptUtil.encrypt(EncryptUtil.getDefaultKey(), oldPassword).equals(user.getUserLoginPassword())) {
				this.logger.error("旧密码不正确!");
				result.setMessage("旧密码不正确!");
				result.setResult(3);
				return result;
			}
			if (StringUtils.isEmpty(newPassword)) {
				this.logger.error("请输入新密码!");
				result.setMessage("请输入新密码!");
				result.setResult(3);
				return result;
			}
			if (StringUtils.isEmpty(confirmPassword)) {
				this.logger.error("请输入确认密码!");
				result.setMessage("请输入确认密码!");
				result.setResult(3);
				return result;
			}
			if (!newPassword.equals(confirmPassword)) {
				this.logger.error("确认密码与新密码不一致!");
				result.setMessage("确认密码与新密码不一致!");
				result.setResult(3);
				return result;
			}
			user.setUserLoginPassword(EncryptUtil.encrypt(EncryptUtil.getDefaultKey(), confirmPassword));
			this.userService.updateUser(user);
			result.setResult(0);
			result.setMessage("OK");
			long endTime = System.currentTimeMillis();
			this.logger.info("result:" + new Gson().toJson(result) + "程序运行时间" + (endTime - startTime) + "ms");
		} catch (Exception e) {
			this.logger.error(e.getMessage(), e);
			result.setMessage(e.getMessage());
			result.setResult(2);
		}
		return result;
	}

	@RequestMapping({ "/logout" })
	@ResponseBody
	public Object logout(HttpServletRequest request, HttpServletResponse response) throws Exception {
		long startTime = System.currentTimeMillis();
		AdminResult result = new AdminResult();
		try {
			String username = this.sessionService.getAttibute(RequestUtils.getCSESSIONID(request, response),
					"USER_SESSION");
			this.logger.info("页面来源:" + request.getHeader("referer") + "客户端:" + request.getHeader("user-agent")
					+ " 客户端ip: " + Utils.getUserRealAddress(request) + " 操作人: " + username);
			this.sessionService.logout(RequestUtils.getCSESSIONID(request, response), "USER_SESSION");
			result.setResult(0);
			result.setMessage("OK");
			long endTime = System.currentTimeMillis();
			this.logger.info("result:" + new Gson().toJson(result) + "程序运行时间" + (endTime - startTime) + "ms");
		} catch (Exception e) {
			this.logger.error(e.getMessage(), e);
			result.setMessage(e.getMessage());
			result.setResult(2);
		}
		return result;
	}
}
